<?php
namespace app\admin\controller;
use app\admin\controller\BaseAdmin;
use think\Db;
use think\Model;
use think\Validate;


class Equipment extends BaseAdmin
{



    /**
     * 订单列表
     * @return mixed
     */
    public function index(){
        if (request()->isAjax()){
            $list = $this->search('equipment');
            return $list;
        }else{
            $pageno = input('param.pageno', 1);
            $this->assign('pageno', $pageno);
            return $this->fetch();
        }
    }




    /**
    *修改
    **/
    public function set(){
        if (request()->isAjax()){
            if(request()->isPost()){
                $post = request()->post();
                if(empty($post)){
                    return ['status'=>false,'message'=>'无数据可操作'];
                }
                
                $id = !empty($post['id'])?$post['id']:0;
                if($id){
                    unset($post['id']);
                    $res = Db::name('equipment')->where('id', $id)->update($post);
                    if($res!==false){
                        return ['status'=>true, 'message'=>'更新成功'];
                    }else{
                        return ['status'=>false,'message'=>'更新失败'];
                    }
                }else{
                    $res = Db::name('equipment')->insertGetId($post);
                    if($res!==false){
                        return ['status'=>true, 'message'=>'新增成功'];
                    }else{
                        return ['status'=>false,'message'=>'新增失败'];
                    }
                }
                
            }else{
                $id = request()->get('id');
                return Db::name('equipment')->where('id', $id)->find();
            }
        }else{
            $pageno = request()->get('pageno')?request()->get('pageno'):1;
            $this->assign('id',request()->get('id'));
            $this->assign('pageno', $pageno);
            return $this->fetch();
        }
    }



    /**
     * 删除
     * @return array
     */
    public function remove(){
        $ids = request()->post('ids');
        if(request()->isAjax()){
            $ids = json_decode($ids, true);
            $result = Db::name('equipment')->delete($ids);
            if($result){
                return ['status'=>true,'message'=>'删除成功!'];
            }else{
                return ['status'=>false,'message'=>'删除失败!'];
            }
        }else{
            new \HttpException(502,'请求不合法');
        }
    }



    /**
    **
    **/
    public function generate()
    {
        require dirname(dirname(dirname(__DIR__))).'/vendor/phpqrcode/phpqrcode.php';
        $id = input('param.id');
        //生成的地址
        $url = THEME.'/qrcode/'.$id;
        // 纠错级别：L、M、Q、H
        $level = 'L';
        // 点的大小：1到10,用于手机端4就可以了
        $size = 10;
        $root = $_SERVER['DOCUMENT_ROOT'];
        $path = 'uploads/down/';
        $filePath = $path.$id.'_'.md5($url).'.png';
        //判断文件是否存在
        if (!is_dir($root.'/'.$path) ) {
            mkdir($root.'/'.$path, 0777, true);
        }
        //生成二维码图片
        \QRcode::png($url, $filePath, $level, $size);
        $ret  = '/'.$filePath;
        if($ret){
            if(file_exists(realpath(dirname(__FILE__).'/../../../').'/public'.$ret) ){
                Db::name('equipment')->where('id', $id)->update(['qrcode'=>$ret]);
            }
            
            return ['status'=>true,'message'=>'已生成!', 'data'=>$ret];
        }else{
            return ['status'=>false,'message'=>'生成失败!'];
        }
    }



    

}